﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace QuanLyDiemDHBK
{
    public partial class ConnectServer : Form
    {
        public static string ipserver = "";
        public ConnectServer()
        {
            InitializeComponent();
        }

        public byte[] SerializeData(Object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, o);
            return ms.ToArray();
        }

        QLDiemClient frmQLDiem = null;
        private void button1_Click(object sender, EventArgs e)
        {
            ipserver = tbip.Text;
            try
            {
                IPAddress[] ipAddress = Dns.GetHostAddresses(tbip.Text);
                IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
                Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSock.Connect(ipEnd);
                byte[] senddata = new byte[1024];
                senddata = SerializeData("ket noi roi ban");
                clientSock.Send(senddata);
                this.Hide();
                MessageBox.Show("Đã kết nối đến Server thành công");
                frmQLDiem = new QLDiemClient();
                frmQLDiem.Show();
                clientSock.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("          KẾT NỐI THẤT BẠI\n" + ex.Message);
            }
        }

        public string getip()
        {
            return ipserver;
        }

        private void ConnectServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}
